-------------------------------------------------------------------------------------
-- -*- coding: utf-8 -*-
--
-- @author:   zxf
-- @email:    07291@qq.com
-- @date:     2024/6/6
-- @module:   dungeon
-- @describe: 副本
-- @version:  v1.0
--

-------------------------------------------------------------------------------------

---@class dungeon
local dungeon = {}
local this = dungeon

local import = import
local dungeon_unit = dungeon_unit
local dungeon_ctx = dungeon_ctx

---@type dungeon_cfg
local dungeon_cfg = import("game/config/dungeon_cfg")

-- 获取副本配置
function this.get_dungeon_config(mainName, subName)
    local DUN_INFO = dungeon_cfg.DUN_INFO
    for k, mainInfo in pairs(DUN_INFO) do
        if k == mainName then
            for _, subInfo in pairs(mainInfo.subInfoList) do
                if subInfo.area_name == subName then
                    subInfo.nType = mainInfo.nType
                    return subInfo
                end
            end
        end
    end
    return nil
end

-- 得到副本列表
function this.get_dungeon_list()
    local list = dungeon_unit.dungeon_list()
    local ret = nil
    for i = 1, #list do
        local main_obj = list[i]
        if dungeon_ctx:init(main_obj) then
            local stage_list = dungeon_unit.dungeon_stage_list(dungeon_ctx:id())
            local main_name = dungeon_ctx:name()
            for j = 1, #stage_list do
                local stage_obj = stage_list[j]
                if dungeon_ctx:init(stage_obj) then
                    -- 只找有配置的副本
                    local cfg = this.get_dungeon_config(main_name, dungeon_ctx:name())
                    if cfg then
                        if not ret then
                            ret = {}
                        end
                        -- 将子副本的信息添加到子副本信息表中
                        table.insert(ret, {
                            stage_obj = stage_obj,
                            sub_id = dungeon_ctx:id(),
                            main_id = dungeon_ctx:main_dungeon_id(),
                            name = dungeon_ctx:name(),
                            can_enter = dungeon_ctx:can_enter(),
                            main_name = main_name,
                            cfg = cfg,
                            min_level = cfg.level,
                            frame = cfg.frame,
                            max_level = cfg.max_level,
                            nType = cfg.nType
                        })
                    end
                end
            end
        end
    end
    return ret
end

-- 是否在地下城
function this.is_in_dungeon(owner)
    local curMapId = owner.mapId
    local DUN_INFO = dungeon_cfg.DUN_INFO
    for k, v in pairs(DUN_INFO) do
        for _, subInfo in ipairs(v.subInfoList) do
            if subInfo.map_id == curMapId then
                return true
            end
        end
    end
    return false
end



return this
